home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’90 / Busy Box / Sources / ball.c next >
Encoding:
C/C++ Source or Header  |  1990-06-14  |  1.3 KB  |  52 lines  |  [TEXT/KAHL]

  1. /* file: balls.c */
  2. /* by Dan Gilliam */
  3.  
  4. #include "busybox.h"
  5.  
  6. static Rect    innerRect;
  7. static Rect ballRect;
  8. static Point deltaPt;    /* stores our movement offset */
  9. static Point lastPt;    /* stores our, uh, last point. */
  10.  
  11. #define ballRadius  3
  12.  
  13.  
  14. void InitSillyBallz(Rect *drawArea, int16 objectID) {
  15.     /* set-up stuff */
  16.     
  17.     innerRect = *drawArea;
  18.     innerRect.top += ballRadius + 2;
  19.     innerRect.left += ballRadius + 2;
  20.     InsetRect(&innerRect, ballRadius, ballRadius);
  21.  
  22.     SetRect(&ballRect, -ballRadius, -ballRadius, ballRadius, ballRadius); /* set center of ball to 0,0 */
  23.     deltaPt.h = 2 * ballRadius;    /* get an initial direction and speed*/
  24.     deltaPt.v = 2 * ballRadius;
  25.     lastPt.h = innerRect.right/2;
  26.     lastPt.v = innerRect.bottom/2;
  27.     
  28.     OffsetRect(&ballRect, lastPt.h, lastPt.v);    /* move ballRect to the pt */
  29.     
  30. }  /* InitSillyBallz */
  31.  
  32.  
  33. void DrawSillyBallz ( int16 objectID) {
  34.     /* do the bouncing ball */
  35.     
  36.     Point newCenter;
  37.     
  38.     
  39.     AddPt(deltaPt, &lastPt);    /* calculate where we're going*/
  40.     if ((lastPt.h < innerRect.left) || (lastPt.h > innerRect.right)) 
  41.         deltaPt.h = -deltaPt.h + (Random() % 2);
  42.     
  43.     if ((lastPt.v > innerRect.bottom) || (lastPt.v < innerRect.top)) 
  44.         deltaPt.v = -deltaPt.v + (Random() % 2);
  45.     
  46.     
  47.     EraseOval(&ballRect);
  48.     OffsetRect(&ballRect, deltaPt.h, deltaPt.v);    
  49.     PaintOval(&ballRect);
  50.  
  51. }  /* DoSillyBallz */
  52.